package domain;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class MyJDBC {
	protected static String driverName = "com.mysql.jdbc.Driver";
	protected static String url = "jdbc:mysql://localhost/mdoc";
	protected static String login = "root";
	protected static String pass = "root";
	
	protected Connection connection = null; 
	
	MyJDBC(){
		
	}
	
	public boolean connect(){
		try {
			Class.forName(driverName);
			connection = DriverManager.getConnection(url, login, pass);
			return true;
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public void disconnect(){
		if(connection != null)
			try {
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
	}
	
	public ResultSet sendRequest(String req){
		if(connection == null)
			return null;
		try {
			Statement s = connection.createStatement();
			s.execute(req);
			return s.getResultSet();
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}

}
